package com.example.demo3.dao.impl;

import com.example.demo3.dao.UserDao;
import com.example.demo3.model.User;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

@Repository
public class UserDaolmpl implements UserDao {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public int addUer(User user) {
        String sql = "INSERT into `user` (`user`.user_name,`user`.`status`) VALUES(?,?);";
        int update = jdbcTemplate.update(sql,user.getUserName(),user.getStatus());
        return update;
    }

    @Override
    public int update(User user) {   // 更新指定用户名的状态信息
        String sql = "UPDATE `user` SET status=? WHERE user_name=?;";
        return jdbcTemplate.update(sql,user.getStatus(),user.getUserName());
    }

    @Override
    public int delete(User user) {      // 删除指定用户名的用户信息
        String sql = "DELETE FROM `user` WHERE user_name=?;";
        return jdbcTemplate.update(sql,user.getUserName());
    }

    @Override
    public int count(User user) {     // 统计指定状态的用户数据
        String sql = "SELECT COUNT(*) FROM `user` WHERE `status`=?;";
        return jdbcTemplate.queryForObject(sql,Integer.class,user.getStatus());
    }

    @Override
    public List<User> getList(User user) {     // 获取所有某种状态的用户列表
        String sql = "SELECT * FROM `user` WHERE `status`=?;";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class),user.getStatus());

    }

    @Override
    public User getUser(User user) {      // 查询指定id的用户
        String sql = "SELECT * FROM `user` WHERE ``user_id=?;";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),user.getUserId());
    }

    @Override
    public void batchAddUser(List<Object[]> batchArgs) {       // 批量新增用户
        String sql = "INSERT into `user` (`user`.user_name,`user`.`status`) VALUES(?,?);";
        jdbcTemplate.batchUpdate(sql, batchArgs);
    }
}
